<?php

namespace App\Service\WorkOrder\Message;

use App\Enums\WorkOrder\OrderEnum;
use App\Http\Middleware\OpenShop\UserPermissionAuth;
use App\Service\Common\SmsService;
use App\Service\OpenShop\Feishu\MessageSendService;
use Illuminate\Support\Facades\Log;

class SendSmsService extends BaseMessageService
{
    /**
     * 工单申请结果短信
     * @param bool $result true完成 false关闭
     * @return void
     */
    public function workOrderApplyResult(bool $result)
    {
        if ($this->workOrderApplyMainModel->problemPid == OrderEnum::PROBLEM_PID_5) {

            // 功能开通
            if ($result) {
                $template = config('sms.template.workOrderOpenFunctionFinish');
                $params   = [
                    'WorkOrderNo' => $this->workOrderApplyMainModel->workOrderNo,
                ];
            } else {
                $template = config('sms.template.workOrderOpenFunctionClose');
                $params   = [
                    'name'               => $this->name,
                    'WorkOrderType'      => (string)$this->workOrderApplyMainModel->problemPidText,
                    'WorkOrderChildType' => $this->workOrderApplyMainModel->problemChildIdText,
                ];
            }

        } else {

            // 问题反馈
            if ($result) {
                $template = config('sms.template.workOrderProblemFinish');
            } else {
                $template = config('sms.template.workOrderProblemClose');
            }

            $params = [
                'name'          => $this->name,
                'WorkOrderType' => (string)$this->workOrderApplyMainModel->problemPidText,
                'WordOrderCode' => $this->workOrderApplyMainModel->workOrderNo,
            ];
        }

        if (!$this->phone) {
            Log::warning('workOrderApplyResult phone empty :' . $this->workOrderApplyMainModel->workOrderNo);
            return;
        }

        try {
            (new SmsService())->aliyunSend($this->phone, $template, $params);
        } catch (\Exception $e) {
            Log::warning("workOrderApplyResult error: {$e->getMessage()}");
            Log::warning($e);
        }
    }
}
